package com.example.experiment4.web.client;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

@Controller
@Api(tags = "Login Controller", description = "登录及异常页面处理相关接口")
public class LoginController {
    // 登录页面相关逻辑
    @GetMapping(value = "/login")
    @ApiOperation("访问登录页面，处理登录相关跳转逻辑")
    public String login(HttpServletRequest request, Map<String, Object> map) {
        String referer = request.getHeader("Referer");
        String url = request.getParameter("url");
        if (url != null && !url.equals("")) {
            map.put("url", url);
        } else if (referer != null && referer.contains("/login")) {
            map.put("url", "");
        } else {
            map.put("url", referer);
        }
        return "comm/login";
    }

    // 异常页面处理相关逻辑
    @GetMapping(value = "/errorPage/{page}/{code}")
    @ApiOperation("根据页面和代码跳转到对应异常页面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页面名称", required = true),
            @ApiImplicitParam(name = "code", value = "错误代码", required = true)
    })
    public String AccessExceptionHandler(@PathVariable("page") String page, @PathVariable("code") String code) {
        return page + "/" + code;
    }
}